<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="药品名称" prop="drugName">
        <el-input
          v-model="queryParams.drugName"
          placeholder="请输入药品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:drugverify:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:drugverify:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="drugverifyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="编码" align="center" prop="code" />
      <el-table-column label="药品id" align="center" prop="drugId" />
      <el-table-column label="药品名称" align="center" prop="drugName" />
      <el-table-column label="住院号" align="center" prop="admissionId" />
      <el-table-column label="审核结果" align="center" prop="verify" :formatter="verifyFormat"/>
      <el-table-column label="审核时间" align="center" prop="verifyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.verifyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="verifyBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['system:drugverify:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:drugverify:edit']"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改药品审核对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="药品名称" prop="code">
          {{form.drugName}}
        </el-form-item>
        <el-form-item label="规则表达式" prop="drugId">
          {{form.expression}}
        </el-form-item>
        <el-form-item label="规则列表" prop="drugName">
          <div v-for="item of form.resolves" :key="item">
            {{item.code}} | {{item.keywords||''}}{{item.modelId||''}} | {{item.semanteme||''}} | {{matchTypeFormat(item.matchType||'')}} | {{item.targetValue||''}}
          </div>
        </el-form-item>
        <el-form-item label="校验内容">
            {{form.content}}
        </el-form-item>
        <el-form-item label="规则结果">
            {{form.verifyInfo}}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDrugverify, getDrugverify, verify, delDrugverify, addDrugverify, updateDrugverify, verifyDetail } from "@/api/system/drugverify";

export default {
  name: "Drugverify",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 药品审核表格数据
      drugverifyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: undefined,
        drugId: undefined,
        drugName: undefined,
        admissionId: undefined,
        verify: undefined,
        verifyTime: undefined,
        verifyBy: undefined
      },
      matchTypeOptions: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("drug_match_type").then(response => {
      this.matchTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询药品审核列表 */
    getList() {
      this.loading = true;
      listDrugverify(this.queryParams).then(response => {
        this.drugverifyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        code: undefined,
        drugId: undefined,
        drugName: undefined,
        admissionId: undefined,
        verify: undefined,
        verifyTime: undefined,
        verifyBy: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    matchTypeFormat(matchType) {
      return this.selectDictLabel(this.matchTypeOptions, matchType);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加药品审核";
    },
    handleView(row) {
      const params = {
        admissionId: row.admissionId,
        drugName: row.drugName,
        verifyId: row.id
      };
      verifyDetail(params).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "药品审核展示";
      });

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const params = {
        admissionId: row.admissionId,
        drugName: row.drugName,
        verifyId: row.id
      };
      verify(params).then(response => {
        if (response.code === 200) {
          this.msgSuccess("校验结果:"+response.data);
          this.getList();
        }
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateDrugverify(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addDrugverify(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    verifyFormat(row, column) {
      return row.verify === 1 ? '通过' : row.verify === 2 ? '未通过' : '未处理';
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除药品审核编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDrugverify(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/drugverify/export', {
        ...this.queryParams
      }, `system_drugverify.xlsx`)
    }
    
  }
};
</script>